<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${config.basePackage}.mapper.${table.className}Mapper">

    <select id="listByConditions" resultType="${config.basePackage}.model.entity.${table.className}Entity">
        SELECT
        <#list table.columns as column>`${column.columnName}`<#if column_has_next>, </#if></#list>
        FROM ${table.tableName}
        <where>
            <#assign hash='#'>
            <#list table.columns as column>
                <if test="${column.propertyName} != null<#if (column.dataType?exists) && (column.dataType?lower_case == 'varchar')> and ${column.propertyName} != ''</#if>">AND `${column.columnName}` = ${hash}{${column.propertyName}}</if>
            </#list>
        </where>
    </select>

    <insert id="save" parameterType="${config.basePackage}.model.entity.${table.className}Entity">
        INSERT INTO ${table.tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list table.columns as column>
            <if test="${column.propertyName} != null<#if (column.dataType?exists) && (column.dataType?lower_case == 'varchar')> and ${column.propertyName} != ''</#if>">`${column.columnName}`,</if>
            </#list>
        </trim>
        <#assign hash='#'>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#list table.columns as column>
            <if test="${column.propertyName} != null<#if (column.dataType?exists) && (column.dataType?lower_case == 'varchar')> and ${column.propertyName} != ''</#if>">${hash}{${column.propertyName}},</if>
            </#list>
        </trim>
    </insert>

    <update id="update" parameterType="${config.basePackage}.model.entity.${table.className}Entity">
        UPDATE ${table.tableName}
        <#assign hash='#'>
        <set>
        <#list table.columns as column>
            <#if !column.primaryKey>
            <if test="${column.propertyName} != null<#if (column.dataType?exists) && (column.dataType?lower_case == 'varchar')> and ${column.propertyName} != ''</#if>">`${column.columnName}` = ${hash}{${column.propertyName}},</if>
            </#if>
        </#list>
        </set>
        <#list table.columns as column>
        <#if column.primaryKey>
        WHERE `${column.columnName}` = ${hash}{${column.propertyName}}
        </#if>
        </#list>
    </update>
</mapper>
